package com.kc.web.controller.business;

import com.kc.business.domain.BizScenicBusinessType;
import com.kc.business.service.IBizScenicBusinessTypeService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 * 类别列表
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/9/9
 */
@RestController
@RequestMapping("/business/scenicBusinessType")
public class BizScenicBusinessTypeController extends BaseController {

    @Autowired
    IBizScenicBusinessTypeService bizScenicBusinessTypeService;

    /**
     * 获取商家类别列表
     */
    @PreAuthorize("@ss.hasPermi('business:scenicBusinessType:list')")
    @GetMapping("/list")
    public AjaxResult list(BizScenicBusinessType bizScenicBusinessType) {
        bizScenicBusinessType.setIsDeleted(0);
        List<BizScenicBusinessType> list = bizScenicBusinessTypeService.getList(bizScenicBusinessType);
        return AjaxResult.success(bizScenicBusinessTypeService.buildTypes(list));
    }

    /**
     * 新增商家类别
     */
    @PreAuthorize("@ss.hasPermi('business:scenicBusinessType:add')")
    @Log(title = "商家类别管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizScenicBusinessType bizScenicBusinessType) {
        return toAjax(bizScenicBusinessTypeService.add(bizScenicBusinessType));
    }

    /**
     * 新增商家类别
     */
    @PreAuthorize("@ss.hasPermi('business:scenicBusinessType:edit')")
    @Log(title = "商家类别管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody BizScenicBusinessType bizScenicBusinessType) {
        return toAjax(bizScenicBusinessTypeService.edit(bizScenicBusinessType));
    }

    /**
     * 删除商家类别
     */
    @PreAuthorize("@ss.hasPermi('business:scenicBusinessType:remove')")
    @Log(title = "商家类别管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeId}")
    public AjaxResult remove(@PathVariable Integer typeId) {
        BizScenicBusinessType queryBusinessType = new BizScenicBusinessType();
        queryBusinessType.setPid(typeId);
        queryBusinessType.setIsDeleted(0);
        List<BizScenicBusinessType> list = bizScenicBusinessTypeService.getList(queryBusinessType);
        if (list.size() > 0) {
            return AjaxResult.error("请先删除所有子分类");
        }
        BizScenicBusinessType updateBusinessType = new BizScenicBusinessType();
        updateBusinessType.setId(typeId);
        updateBusinessType.setIsDeleted(1);
        return toAjax(bizScenicBusinessTypeService.edit(updateBusinessType));
    }
}
